package com.minimalist.basic.controller.lindi;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.http.HttpStatus;
import com.minimalist.common.config.mybatis.bo.PageResp;
import com.minimalist.common.entity.lindi.vo.enterpriseaccount.LindiEnterpriseAccountQueryVO;
import com.minimalist.common.entity.lindi.vo.enterpriseaccount.LindiEnterpriseAccountVO;
import com.minimalist.common.feign.client.lindi.LindiEnterpriseAccountFeignClient;
import com.minimalist.common.response.R;
import com.minimalist.common.utils.Add;
import com.minimalist.common.utils.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 11987
 */
@Validated
@RestController
@Tag(name = "林地指标交易平台_企业用户管理")
@RequestMapping("/basic/lindi/enterpriseAccount")
public class LindiEnterpriseAccountController{

    @Autowired
    private LindiEnterpriseAccountFeignClient feignClient;

    @PostMapping("/addEnterpriseAccount")
    @SaCheckPermission("lindi:enterpriseAccount:add")
    @Operation(summary = "添加企业注册信息")
    public R<?> addEnterpriseAccount(
            @RequestBody @Validated(Add.class)
            LindiEnterpriseAccountVO enterpriseAccountVO){
        enterpriseAccountVO.setCreateId(StpUtil.getLoginIdAsLong());
        enterpriseAccountVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        enterpriseAccountVO.setCreateTime(now);
        enterpriseAccountVO.setUpdateTime(now);
        return feignClient.addEnterpriseAccount(enterpriseAccountVO);
    }

    @DeleteMapping("/deleteEnterpriseAccountByEnterpriseAccountId")
    @SaCheckPermission("lindi:enterpriseAccount:delete")
    @Operation(summary = "删除企业注册信息 -> 根据企业注册信息ID删除")
    public R<?> deleteEnterpriseAccountByEnterpriseAccountId(
            @RequestParam("enterpriseAccountId")
            @NotNull(message = "企业注册信息ID不能为空")
            @Parameter(name = "enterpriseAccountId", required = true
                    , description = "企业注册信息ID") Long enterpriseAccountId){
        return feignClient.deleteEnterpriseAccountByEnterpriseAccountId(
                enterpriseAccountId);
    }

    @PutMapping("/updateEnterpriseAccountByEnterpriseAccountId")
    @SaCheckPermission("lindi:enterpriseAccount:update")
    @Operation(summary = "修改企业注册信息")
    public R<?> updateEnterpriseAccountByEnterpriseAccountId(
            @RequestBody @Validated(Update.class)
            LindiEnterpriseAccountVO enterpriseAccountVO){
        enterpriseAccountVO.setCreateId(StpUtil.getLoginIdAsLong());
        enterpriseAccountVO.setUpdateId(StpUtil.getLoginIdAsLong());
        LocalDateTime now = LocalDateTime.now();
        enterpriseAccountVO.setCreateTime(now);
        enterpriseAccountVO.setUpdateTime(now);
        return feignClient.updateEnterpriseAccountByEnterpriseAccountId(
                enterpriseAccountVO);
    }

    @GetMapping("/getPageEnterpriseAccountList")
    @SaCheckPermission("lindi:enterpriseAccount:get")
    @Operation(summary = "查询企业注册信息列表(分页)")
    public R<PageResp<LindiEnterpriseAccountVO>>
    getPageEnterpriseAccountList(LindiEnterpriseAccountQueryVO queryVO){
        return feignClient.getPageEnterpriseAccountList(queryVO);
    }

    @GetMapping("/getEnterpriseAccountByEnterpriseAccountId/{enterpriseAccountId}")
    @SaCheckPermission("lindi:enterpriseAccount:get")
    @Operation(summary = "根据企业注册信息ID查询企业注册信息")
    public R<LindiEnterpriseAccountVO> getEnterpriseAccountByEnterpriseAccountId(
            @PathVariable(value = "enterpriseAccountId")
            @NotNull(message = "企业注册信息ID不能为空")
            @Parameter(name = "enterpriseAccountId", description = "参数ID"
                    , required = true) Long enterpriseAccountId){
        return feignClient.getEnterpriseAccountByEnterpriseAccountId(
                enterpriseAccountId);
    }

    @GetMapping("/getPasswordByEnterpriseMobile/{enterpriseMobile}")
    @SaCheckPermission("lindi:enterpriseAccount:get")
    @Operation(summary = "根据企业手机号查询密码")
    public R<Object> getPasswordByEnterpriseMobile(
            @PathVariable(value = "enterpriseMobile")
            @NotNull(message = "企业手机号不能为空")
            @Parameter(name = "enterpriseMobile", description = "参数ID"
                    , required = true) String enterpriseMobile){
        R<String> passwordResult = feignClient.getPasswordByEnterpriseMobile(enterpriseMobile);
        if (passwordResult.getCode() != HttpStatus.HTTP_OK){
            return R.fail(passwordResult.getCode(), passwordResult.getMessage());
        }
        Map<String, Object> map = new HashMap<>(1);
        map.put("password", passwordResult.getData());
        return R.ok(map);
    }

    @GetMapping("/getEnterpriseAccountList")
    @SaCheckPermission("lindi:enterpriseAccount:get")
    @Operation(summary = "获取企业账号列表")
    public R<List<LindiEnterpriseAccountVO>> getEnterpriseAccountList(){
        return feignClient.getEnterpriseAccountList();
    }
}